package com.go.where.manager.action.system;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.user.Menu;
import com.go.where.manager.pojo.user.Permission;
import com.go.where.manager.pojo.user.Role;
import com.go.where.manager.service.system.MenuBuildService;
import com.go.where.manager.service.system.PermissionService;
import com.go.where.manager.service.system.RoleService;
import com.go.where.manager.util.CommonUtil;
import com.google.gson.Gson;

public class RoleAction extends SuperAction {

	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(RoleAction.class);
	@Resource(name = "roleService")
	private RoleService roleService;
	@Resource(name = "menuBuildService")
	private MenuBuildService menuBuildService;
	@Resource(name = "permissionService")
	private PermissionService permissionService;
	private Role role;
	private String roleId;
	private String flag;
	private Role roleQuery;
	private List<Menu> menus;
	private List<String> codes;

	public Pagination<?> page;

	Gson gson = CommonUtil.gson();

	// 添加
	public String add() {
		menus = menuBuildService.getAllMenu();
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			role.setMenuIdList(codes);
			roleService.save(role);
			redirectionUrl = "role!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("保存信息异常！");
			return this.add();
		}

	}

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (roleQuery == null) {
			roleQuery = new Role();
			roleQuery.setPageSize(Constants.INIT_PAGESIZE);
			roleQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = roleService.queryPage(roleQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}

		return LIST;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		role = roleService.queryById(roleId);
		List<Permission> ps = permissionService.queryByRoleId(roleId);
		menus = menuBuildService.getAllMenu();
		for (Menu m : menus) {
			for (Permission p1 : ps) {
				if (m.getMenuId().equals(p1.getMenuId())) {
					m.setOpt(true);
					break;
				} else {
					m.setOpt(false);
				}
			}
		}
		return INPUT;
	}

	public String edit() throws Exception {
		try {
			role.setMenuIdList(codes);
			roleService.update(role);
			redirectionUrl = "role!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！");
			roleId = role.getRoleId();
			return this.toEdit();
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		try {
			roleService.delete(roleId);
		} catch (Exception e) {
			log.error("删除信息异常", e);
			addActionError("删除信息异常！");
			return LIST;
		}
		redirectionUrl = "role!list.htm";
		return SUCCESS;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Role getRoleQuery() {
		return roleQuery;
	}

	public void setRoleQuery(Role roleQuery) {
		this.roleQuery = roleQuery;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public List<Menu> getMenus() {
		return menus;
	}

	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}

	public List<String> getCodes() {
		return codes;
	}

	public void setCodes(List<String> codes) {
		this.codes = codes;
	}

}
